import Vue from 'vue'
import VueRouter from 'vue-router'
import {getItem} from '@/utils/storage.js'

Vue.use(VueRouter)

const routes = [{
		path: '/',
		redirect: '/home'
	},
	{
		path: '/login',
		component: () => import('@/views/login')
	}, {
		path: '/',
		component: () => import('@/views/layout'),
		children: [{
			path: '/home',
			component: () => import('@/views/home')
		}, {
			path: '/my',
			component: () => import('@/views/my')
		}]
	}, {
		path: '/search',
		component: () => import('@/views/search')
	}, {
		path: '/article/:articleId',
		component: () => import('@/views/article'),
		props: true
	}, {
		path: '/user',
		component: () => import('@/views/user'),
	}, {
		path: '/chat',
		component: () => import('@/views/chat'),
	}
]

const router = new VueRouter({
	routes
})

router.beforeEach((to, from, next) => {
	if(getItem('TOUTIAOUSER') && to.path === '/login') {
		next('/')
	} else {
		next()
	}
})

export default router
